package com.starry.module.member.core.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.starry.core.common.constants.CommonError;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.core.common.execption.ServiceException;
import com.starry.db.core.enums.DBEnableDisableStatusEnum;
import com.starry.db.core.query.LambdaQueryWrapperX;
import com.starry.module.member.core.member.entity.MemberUser;
import com.starry.module.member.core.member.mapper.MemberUserMapper;
import com.starry.module.member.core.member.service.MemberUserService;
import com.starry.module.system.api.member.domain.qo.MemberUserPageQo;
import com.starry.module.system.api.member.domain.qo.MemberUserQo;
import com.starry.module.system.api.member.domain.vo.MemberUserDtlVo;
import com.starry.module.system.api.member.domain.vo.MemberUserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 会员用户-服务实现
 *
 * @author xiaoke
 */
@Service("memberUserService")
public class MemberUserServiceImpl implements MemberUserService {

    private final MemberUserMapper memberUserMapper;

    public MemberUserServiceImpl(MemberUserMapper memberUserMapper) {
        this.memberUserMapper = memberUserMapper;
    }

    @Override
    public PageVo<MemberUserVo> page(MemberUserPageQo pageQo) {
        PageVo<MemberUser> page = memberUserMapper.selectPage(pageQo);
        List<MemberUserVo> vos = BeanUtil.copyToList(page.getRows(), MemberUserVo.class);
        return new PageVo<>(vos, page.getTotal());
    }

    @Override
    public MemberUserDtlVo getByIdDtl(Long id) {
        MemberUser memberUser = memberUserMapper.selectById(id);
        if (memberUser == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        return BeanUtil.toBean(memberUser, MemberUserDtlVo.class);
    }

    @Override
    public MemberUser getById(Long id) {
        return memberUserMapper.selectById(id);
    }

    @Transactional
    @Override
    public Long save(MemberUserQo memberUserQo) {
        LambdaQueryWrapperX<MemberUser> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .eq(MemberUser::getUsername, memberUserQo.getUsername());
        if (memberUserMapper.selectCount(wrapper) > 0) {
            throw new ServiceException(CommonError.DATA_EXISTS.getMsg());
        }
        MemberUser memberUser = BeanUtil.toBean(memberUserQo, MemberUser.class);
        memberUser.setId(null);
        memberUserMapper.insert(memberUser);
        return memberUser.getId();
    }

    @Transactional
    @Override
    public void updateById(MemberUserQo memberUserQo) {
        if (memberUserQo.getId() == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        LambdaQueryWrapperX<MemberUser> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .ne(MemberUser::getId, memberUserQo.getId())
                .and(w ->
                        w.eq(MemberUser::getUsername, memberUserQo.getUsername())
                );
        if (memberUserMapper.selectCount(wrapper) > 0) {
            throw new ServiceException(CommonError.DATA_EXISTS.getMsg());
        }
        MemberUser memberUser = memberUserMapper.selectById(memberUserQo.getId());
        if (memberUser == null) {
            throw new ServiceException(CommonError.DATA_NOT_EXISTS.getMsg());
        }
        BeanUtil.copyProperties(memberUserQo, memberUser);
        memberUserMapper.updateById(memberUser);
    }


    @Override
    public MemberUser getByValue(String value) {
        return memberUserMapper.selectByValue(value);
    }

    @Override
    public MemberUser initialUser(MemberUser memberUser) {
        memberUser.setRegisterTime(LocalDateTime.now());
        memberUserMapper.insert(memberUser);
        return memberUser;
    }

    @Transactional
    @Override
    public void removeByIds(Set<Long> idList) {
        if (idList.isEmpty()) return;
        memberUserMapper.deleteByIds(idList);
    }

    @Transactional
    @Override
    public void enable(Set<Long> idList) {
        if (idList.isEmpty()) return;
        memberUserMapper.updateBatch(setMemberUserStatus(idList, DBEnableDisableStatusEnum.ENABLE));
    }

    @Transactional
    @Override
    public void disable(Set<Long> idList) {
        if (idList.isEmpty()) return;
        memberUserMapper.updateBatch(setMemberUserStatus(idList, DBEnableDisableStatusEnum.DISABLE));
    }


    private Set<MemberUser> setMemberUserStatus(Set<Long> idList, DBEnableDisableStatusEnum status) {
        Set<MemberUser> memberUsers = new HashSet<>();
        idList.forEach(item -> {
            MemberUser memberUser = new MemberUser();
            memberUser.setId(item);
            memberUser.setStatus(status.getCode());
            memberUsers.add(memberUser);
        });
        return memberUsers;
    }


}

